
#include "replication_pad2d_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"   //GetPlatformInfo类接口
#include <typeinfo>


namespace optiling {

static ge::graphStatus TilingFunc(gert::TilingContext* context)
{
    ReplicationPad2dTilingData tiling;

    auto shape_x = context->GetInputShape(0)->GetOriginShape();
    auto shape_paddings =  context->GetInputShape(1)->GetOriginShape();
    auto shape_y = context->GetOutputShape(0)->GetOriginShape();

    int32_t x_ndarray[20];       //用来存储每一维的数据大小
    int32_t x_dimensional;       //维度数
    int32_t size = 1;            //总的元素个数
    int32_t dim_dimensional;       //维度数

    x_dimensional = shape_x.GetDimNum();
    //printf("x_dimensional is %d\n",x_dimensional);
    for(int i = 0; i < x_dimensional; i++)
    {
        x_ndarray[i] = shape_x.GetDim(i);
        size *= x_ndarray[i];
    }

    dim_dimensional = shape_paddings.GetDim(0);


    //printf("dim_dimensional is %d\n",dim_dimensional);
    tiling.set_size(size);
    tiling.set_x_ndarray(x_ndarray);
    tiling.set_x_dimensional(x_dimensional);
    tiling.set_dim_dimensional(dim_dimensional);

    context->SetBlockDim(1);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    // size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    // currentWorkspace[0] = 0;
    return ge::GRAPH_SUCCESS;
}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class ReplicationPad2d : public OpDef {
public:
    explicit ReplicationPad2d(const char* name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("paddings")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");

    }
};

OP_ADD(ReplicationPad2d);
}
